/*
@description: implement all ContraEntity method
@author: TriLVH
*/

#include "ContraEntity.h"

// ContraEntity constructor with x,y
ContraEntity::ContraEntity(int id, double x, double y) {
	_data = new ContraData(id, x, y);
	_state = new ContraState(_data);
	_physics = new ContraPhysics(_data);
}

// Get contra data
ContraData* ContraEntity::getData() {
	return this->_data;
}

// Get abstract data
AbstractData* ContraEntity::getAbstractData() {
	return this->_data;
}

// Update contra entity
void ContraEntity::update(long time) {
	// udpate current sprites with current state
	_state->update();
	// update physics of this object
	_physics->update();	
}

// draw contra entity
void ContraEntity::draw(long time) {
	// draw current sprite state
	_state->draw(time);
}

// ConstraEntity state behavior

// Contra stand avoid
void ContraEntity::stand() {
	if (_data->isDead) return;
	_state->stand();
	_physics->stand();
}

// Contra stand up avoid
void ContraEntity::standUp() {
	if (_data->isDead) return;
	_state->standUp();
}

// Contra move right
void ContraEntity::moveRight() {
	if (_data->isDead) return;
	_state->moveRight();
	_physics->moveRight();
}

// Contra move left
void ContraEntity::moveLeft() {
	if (_data->isDead) return;
	_state->moveLeft();
	_physics->moveLeft();
}

// Contra fire
void ContraEntity::fire(bool onHold) {
	if (_data->isDead) return;
	// If key down on hold the key and current bullet type is bullet one
	if(onHold && (_data->bulletContainer->getCurrentBulletType() == _ID_BULLET_LEVEL_1)) {
		return;
	}
	_state->fire();
}

// fire when sitting
void ContraEntity::fireSit() {
	if (_data->isDead) return;
}

// contra sit state
void ContraEntity::sit() {
	if (_data->isDead) return;
	_state->sit();
}

// Contra fly up with start vx
void ContraEntity::fly(bool onHold) {
	if (_data->isDead) return;
	if (onHold) return;
	_physics->jumping();
	_state->fly();
	_data->onJump = true;
}

// Init contra data
void ContraEntity::initData() {
	_data->initData();
}

void ContraEntity::updateData(int type, int action) {
	if (_data->isDead) return;
	if (type == 0) {
		if (action == 1)
			_physics->upgradeBullet(_ID_BULLET_LEVEL_3);
		else if (action == 2) {
			_physics->upgradeBullet(_ID_BULLET_LEVEL_2);
		} else {
			_physics->upgradeBullet(_ID_BULLET_LEVEL_1);
		}
	}
	if (type == 1) {
		// update data when contra reach to the land
		_state->setState(1);
		_data->updateData(ACTIONS_STOP_FLYING, 0);

	} else if(type == (int)ACTIONS_IN_WATER) {
		// update data for swim in water		
		_state->setState(2);
	}

	if (type == ACTIONS_IS_HURT) {
		_state->setState(3);
	}

	if (type == ACTIONS_HIT_ITEM) {
		int item = rand() % 2 + _ID_BULLET_LEVEL_2;
		_physics->upgradeBullet(item);
	}

}